package com.example.piece.ui.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.databinding.ViewDataBinding
import com.therouter.router.Autowired
import com.therouter.router.Route
import com.example.base.aop.click.RepeatClick
import com.example.base.broadcast.Event
import com.example.base.broadcast.EventBusUtil
import com.example.base.ext.getViewModel
import com.example.base.ext.toast
import com.example.base.vm.onError
import com.example.piece.App
import com.example.piece.R
import com.example.piece.constant.Key
import com.example.piece.constant.Key.EventKey.EVENT_UPDATE_SUC
import com.example.piece.constant.RoutePath
import com.example.piece.databinding.ActivityUpDataNameAndSignBinding
import com.example.piece.databinding.ActivityUserInfoBinding
import com.example.piece.ui.adapter.JuBaoAdapter
import com.example.piece.vm.UserViewModel

/**
 * 更新用户昵称 及签名
 */
@Route(path = RoutePath.MAIN_UPDATE_NICKNAME_SIGN_ACTIVITY)
class UpDateNameAndSignActivity :
    Layer3Activity<ActivityUpDataNameAndSignBinding, ViewDataBinding>() {
    override val isTransparentStatusBar: Boolean = true
    override val isLockPortrait: Boolean = true
    override val statusBarAlpha: Int = 0
    override val isDarkMode: Boolean = true
    override val isFitsSystemWindows: Boolean = true
    override val leftIcon1Res: Int = R.drawable.ic_back
    override val centerLayoutRes: Int
        get() = R.layout.activity_up_data_name_and_sign
    override val title: String
        get() = H5_TITLE

    @JvmField
    @Autowired(name = Key.DataKey.H5_TITLE)
    var H5_TITLE: String = ""

    @JvmField
    @Autowired(name = Key.DataKey.CHANGE_USER_INFO_TYPE)
    var CHANGE_USER_INFO_TYPE: Int = 0

    @JvmField
    @Autowired(name = Key.DataKey.CHANGE_USER_INFO_CONTENT)
    var CHANGE_USER_INFO_CONTENT: String = ""

    private val userViewModel: UserViewModel by lazy {
        getViewModel<UserViewModel>()
    }

    override fun onClickLeftIcon1(view: View) {
        onBackPressed()

    }

    override fun initData(savedInstanceState: Bundle?) {
        super.initData(savedInstanceState)
        mCenterViewBinding?.data = CHANGE_USER_INFO_CONTENT
    }

    //提交
    @RepeatClick
    fun update(view: View) {
        val content = mCenterViewBinding?.etContent?.text.toString()
        if (content.isNullOrEmpty()) {
            toast("前输入")
            return
        }

        userViewModel.userInfoUpdate(this, content, (CHANGE_USER_INFO_TYPE.plus(1)).toString()) {
            if (it.code == 200) {
                toast("修改成功")

            } else {
                toast(it.msg)

            }
            getUserInfo()
        } onError {
            toast(it)
        }
    }

    //获取个人信息
    private fun getUserInfo() {
        userViewModel.userInfo(null) {//获取个人信息
            if (it.code == 200) {//获取个人信息
                App.userLoginCodeEntity = it.data
                EventBusUtil.sendEvent(Event<String>(EVENT_UPDATE_SUC, ""))

            }
        } onError {

        }
    }
}